//
//  SearchFoodViewController.swift
//  FridgeSolo
//
//  Created by LiuXing on 2017/6/11.
//  Copyright © 2017年 kumi.com. All rights reserved.
//

let foodCellIdentifier = "FoodTableViewCell"

import UIKit

class SearchFoodViewController: UIViewController {
    
    var titleString: String? {
        didSet {
            title = titleString
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupHeadView()
        setupFootView()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    func setupHeadView() {
        searchTopView.addSubview(glassImageView)
        glassImageView.snp.makeConstraints { (make) in
            make.left.equalTo(searchTopView.snp.left).offset(10)
            make.centerY.equalTo(searchTopView.snp.centerY)
            make.width.height.equalTo(15)
        }
        searchTopView.addSubview(textView)
        textView.snp.makeConstraints { (make) in
            make.left.equalTo(glassImageView.snp.right).offset(10)
            make.top.right.equalTo(searchTopView)
            make.bottom.equalTo(searchTopView.snp.bottom).offset(-1)
        }
        searchTopView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalTo(searchTopView)
            make.left.equalTo(searchTopView.snp.left).offset(10)
            make.right.equalTo(searchTopView.snp.right).offset(-10)
            make.height.equalTo(0.5)
        }
        
        tableView.tableHeaderView = searchTopView
    }
    
    func setupFootView() {
        searchFootView.addSubview(addButton)
        addButton.snp.makeConstraints { (make) in
            make.left.top.equalTo(searchFootView).offset(5)
            make.right.equalTo(searchFootView.snp.right).offset(-5)
            make.height.equalTo(45)
        }
        
        searchFootView.addSubview(bottomLineView)
        bottomLineView.snp.makeConstraints { (make) in
            make.bottom.equalTo(searchFootView)
            make.left.equalTo(searchFootView.snp.left).offset(10)
            make.right.equalTo(searchFootView.snp.right).offset(-10)
            make.height.equalTo(0.5)
        }
        
        tableView.tableFooterView = searchFootView
    }
    
    // MARK: - lazy
    lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.separatorStyle = .none
        tableView.register(UINib.init(nibName: "FoodTableViewCell", bundle: nil), forCellReuseIdentifier: foodCellIdentifier)
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
    
    
    private lazy var searchFootView: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 55))
        return view
    }()
    
    private lazy var addButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Quick Add", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btn.setTitleColor(UIColor.colorWithHexString(hex: "3F70BB"), for: .normal)
        btn.backgroundColor = UIColor.white
        btn.layer.cornerRadius = 3
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.colorWithHexString(hex: "3F70BB").cgColor
        btn.layer.masksToBounds = true
        return btn
    }()
    
    private lazy var bottomLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGray
        return view
    }()
    
    private lazy var searchTopView: UIView = {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 44))
        return view
    }()
    
    private lazy var glassImageView: UIImageView = UIImageView(image: UIImage(named: "search"))
    
    private lazy var textView: UITextField = {
        let view = UITextField()
        view.placeholder = "Search for a food"
        return view
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGray
        return view
    }()

}

extension SearchFoodViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: foodCellIdentifier, for: indexPath) as! FoodTableViewCell
        return cell
    }
}

